package com.team.six.bluecatshop.controller;

import com.team.six.bluecatshop.domain.ProductType;
import com.team.six.bluecatshop.domain.SeachProductTypeResponse;
import com.team.six.bluecatshop.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/adminProductType")
public class AminProductTypeController {

    @Autowired
    ProductTypeService productTypeService;
    /**
     * 跳转至商品类型管理
     */
    @GetMapping("/productTypeList")
    public String productTypeGetList(){
        return "administration/product_type_list";
    }

    /**
     * 处理商品类型展示请求
     */
    @GetMapping("/api/productTypeList")
    @ResponseBody
    public SeachProductTypeResponse seachProductTypeResponse(Integer pageNum,Integer pageSize,String keyword){
        return productTypeService.findProductTypeAll(pageNum,pageSize,keyword);
    }

    /**
     * 跳转至新增页面，并展示商品类型名称
     */
    @GetMapping("/addProductTypeShow")
    public String addProductType(Model model){
        List<ProductType> productTypes = productTypeService.findAll();
        model.addAttribute("productTypes",productTypes);
        return "administration/product_type_add";
    }

    /**
     * 跳转至修改页面，并展示商品类型名称
     */
    @GetMapping("/editProductTypeShow")
    public String editProductType(ProductType productType, String ptypeParentName, Model model){
        productType.setPtypeParentName(ptypeParentName);
        model.addAttribute("productType",productType);
        List<ProductType> productTypes = productTypeService.findAll();
        model.addAttribute("productTypes",productTypes);
        return "administration/product_type_add";
    }

    /**
     * 新增修改商品类型请求
     */
    @GetMapping("/addProductType")
    public String addProductType(ProductType productType){
        System.out.println(productType.getPtypeParent());
        if (productType.getPtypeId().equals("")){
            productTypeService.add(productType);
            System.out.println(productType.getPtypeId());
        }else {
            productTypeService.updateByPrimaryKey(productType);
        }
        return "redirect:/adminProductType/productTypeList";
    }

    /**
     * 删除商品类型请求
     */
    @GetMapping("/deleteProductType")
    public String deleteProductType(String ptypeId){
        productTypeService.remove(ptypeId);
        return "redirect:/adminProductType/productTypeList";
    }

    /**
     * 批量删除商品类型请求
     */
    @PostMapping("/batchDeleteProductType")
    @ResponseBody
    public String batchDeleteProductType(String productTypes){
        String[] split = productTypes.split(",");
        for (int i=0;i<split.length;i++){
            System.out.println(split[i]);
            productTypeService.remove(split[i]);
        }
        return "删除成功";
    }
}
